package com.hyf.glidev4.adaoter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.load.MultiTransformation;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.hyf.glidev4.R;
import com.hyf.glidev4.support.TransformationType;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import jp.wasabeef.glide.transformations.BlurTransformation;
import jp.wasabeef.glide.transformations.ColorFilterTransformation;
import jp.wasabeef.glide.transformations.CropCircleTransformation;
import jp.wasabeef.glide.transformations.CropSquareTransformation;
import jp.wasabeef.glide.transformations.CropTransformation;
import jp.wasabeef.glide.transformations.GrayscaleTransformation;
import jp.wasabeef.glide.transformations.MaskTransformation;
import jp.wasabeef.glide.transformations.RoundedCornersTransformation;
import jp.wasabeef.glide.transformations.gpu.BrightnessFilterTransformation;
import jp.wasabeef.glide.transformations.gpu.ContrastFilterTransformation;
import jp.wasabeef.glide.transformations.gpu.InvertFilterTransformation;
import jp.wasabeef.glide.transformations.gpu.KuwaharaFilterTransformation;
import jp.wasabeef.glide.transformations.gpu.PixelationFilterTransformation;
import jp.wasabeef.glide.transformations.gpu.SepiaFilterTransformation;
import jp.wasabeef.glide.transformations.gpu.SketchFilterTransformation;
import jp.wasabeef.glide.transformations.gpu.SwirlFilterTransformation;
import jp.wasabeef.glide.transformations.gpu.ToonFilterTransformation;
import jp.wasabeef.glide.transformations.gpu.VignetteFilterTransformation;

import static com.bumptech.glide.request.RequestOptions.bitmapTransform;

/**
 * Created by Administrator on 2017/12/21.
 */

public class GlideTransformationAdapter extends RecyclerView.Adapter<GlideTransformationAdapter.ViewHolder> {

    private List<Enum> datas;
    private Context mContext;

    public GlideTransformationAdapter(List<Enum> datas, Context context) {
        this.datas = datas;
        mContext = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_recycler_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Enum e = datas.get(position);
        TextView textView = holder.mTextView;
        ImageView imageView = holder.mImageView;
        textView.setText(String.format("%02d", position + 1) + "、" + e);
        RequestBuilder<Bitmap> requestBuilder = Glide.with(mContext).asBitmap().load(R.mipmap.qingxin);
        RequestOptions options = new RequestOptions();
        options.centerCrop();
        if (e == TransformationType.原图) {

        } else if (e == TransformationType.自定义mask) {
            options.apply(bitmapTransform(new MultiTransformation<Bitmap>(new MaskTransformation(R.mipmap.mask_starfish))));
            options.override(600);
        } else if (e == TransformationType.点9mask遮罩) {
            options.apply(bitmapTransform(new MultiTransformation<Bitmap>(new MaskTransformation(R.mipmap.mask_chat_right))));
            options.override(600);
        } else if (e == TransformationType.只显示顶部) {
            options.apply(bitmapTransform(new MultiTransformation<Bitmap>(new CropTransformation(500, 200, CropTransformation.CropType.TOP))));
        } else if (e == TransformationType.只显示中间) {
            options.apply(bitmapTransform(new CropTransformation(500, 200, CropTransformation.CropType.CENTER)));
        } else if (e == TransformationType.只显示底部) {
            options.apply(bitmapTransform(new CropTransformation(500, 200, CropTransformation.CropType.BOTTOM)));
        } else if (e == TransformationType.正方形显示) {
            options.apply(bitmapTransform(new CropSquareTransformation()));
        } else if (e == TransformationType.圆形显示) {
//            options.circleCrop();  也可以显示圆形
            options.apply(bitmapTransform(new CircleCrop()));
        } else if (e == TransformationType.变换颜色) {
            options.apply(bitmapTransform(new ColorFilterTransformation(Color.parseColor("#60ff705e"))));
        } else if (e == TransformationType.灰色滤镜) {
            options.apply(bitmapTransform(new GrayscaleTransformation()));
        } else if (e == TransformationType.圆角效果) {
            options.apply(bitmapTransform(new RoundedCornersTransformation(45, 0)));
        } else if (e == TransformationType.高斯模糊) {
            options.apply(bitmapTransform(new BlurTransformation(23, 5)));
        } else if (e == TransformationType.Toon_什么玩意儿) {
            options.apply(bitmapTransform(new ToonFilterTransformation()));
        } else if (e == TransformationType.棕色) {
            options.apply(bitmapTransform(new SepiaFilterTransformation()));
        } else if (e == TransformationType.对比度) {
            options.apply(bitmapTransform(new ContrastFilterTransformation(2.0f)));
        } else if (e == TransformationType.反色) {
            options.apply(bitmapTransform(new InvertFilterTransformation()));
        } else if (e == TransformationType.像素) {
            options.apply(bitmapTransform(new PixelationFilterTransformation(5f)));
        } else if (e == TransformationType.素描) {
            options.apply(bitmapTransform(new SketchFilterTransformation()));
        } else if (e == TransformationType.漩涡) {
            options.apply(bitmapTransform(new SwirlFilterTransformation()));
        } else if (e == TransformationType.亮度) {
            options.apply(bitmapTransform(new BrightnessFilterTransformation(0.2f)));
        } else if (e == TransformationType.油画效果) {
            options.apply(bitmapTransform(new KuwaharaFilterTransformation(5)));
        } else if (e == TransformationType.Vignette) {
            options.apply(bitmapTransform(new VignetteFilterTransformation()));
        }
        requestBuilder
                .apply(options)
                .into(imageView);
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_recycler_item)
        TextView mTextView;
        @BindView(R.id.iv_recycler_item)
        ImageView mImageView;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
